package cn.com.shopec.erp.product.commo;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Component;

import cn.com.shopec.erp.common.constant.SZSCMConstant;
import cn.com.shopec.erp.common.page.PageFinder;
import cn.com.shopec.erp.common.utils.LogicUtil;
import cn.com.shopec.erp.common.utils.StringUtil;
import cn.com.shopec.erp.product.entity.CommodityCat;
import cn.com.shopec.erp.product.vo.CommodityCatVo;

@Component
public class CommodityCatConvertor {

	public CommodityCatVo getCommodityCatVo(CommodityCat cat) {
		CommodityCatVo catVo = new CommodityCatVo();
		if (LogicUtil.isNotNull(cat)) {
			catVo.setId(cat.getId());
			catVo.setCatNo(cat.getCatNo());
			catVo.setCatName(cat.getCatName());
			catVo.setCatPno(cat.getCatPno());
			catVo.setDescription(cat.getDescription());
			catVo.setDeleteFlag(cat.getDeleteFlag());
			catVo.setCreateTime(cat.getCreateTime());
			catVo.setUpdateTime(cat.getUpdateTime());
			catVo.setNo(cat.getNo());
		}
		return catVo;
	}
	
	public CommodityCat getCommodityCat(CommodityCatVo catVo) {
		CommodityCat cat = new CommodityCat();
		if (LogicUtil.isNotNull(catVo)) {
			cat.setId(catVo.getId());
			if(StringUtil.isExist(catVo.getCatNo())){
				cat.setCatNo(catVo.getCatNo());
			}else{
				cat.setCatNo(SZSCMConstant.genUUID());
			}
			cat.setCatName(catVo.getCatName());
			cat.setCatPno(catVo.getCatPno());
			cat.setDescription(catVo.getDescription());
			cat.setDeleteFlag(SZSCMConstant.DELETE_FLAG_NO);
			SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			cat.setCreateTime(dateFormat.format(new Date()));
			cat.setUpdateTime(dateFormat.format(new Date()));
			cat.setNo(catVo.getNo());
		}
		return cat;
	}

	public PageFinder<CommodityCatVo> getPageFinderCommodityCatVo(
			PageFinder<CommodityCat> pageFinder) {
		
		List<CommodityCatVo> catVoList = getCommodityCatVoList(pageFinder.getData());
		PageFinder<CommodityCatVo> pageFinderVo = new PageFinder<CommodityCatVo>(
				pageFinder.getPageNo(), pageFinder.getPageSize(),
				pageFinder.getRowCount(), catVoList);
		return pageFinderVo;
	}
	
	public List<CommodityCatVo> getCommodityCatVoList(List<CommodityCat> catList){
		List<CommodityCatVo> catVoList = new ArrayList<CommodityCatVo>();
		if(LogicUtil.isNotNull(catList)){
			for (CommodityCat cat : catList) {
				catVoList.add(getCommodityCatVo(cat));
			}
		}
		return catVoList;
	}
}
